/*
 *   Copyright 2011 Joshua Swank
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package name.swank.language;

import java.util.Set;

import com.google.common.base.Preconditions;

public class RegularProductionRule extends ContextFreeProductionRule
{
    private final Symbol nonterminalBody_;
    private final Symbol terminalBody_;

    public RegularProductionRule(Symbol head)
    {
	this(head, null, null);
    }

    public RegularProductionRule(Symbol head, Symbol terminalBody)
    {
	this(head, terminalBody, null);
    }

    public RegularProductionRule(Symbol head, Symbol terminalBody, Symbol nonterminalBody)
    {
	super(head, new SymbolSequence(terminalBody, nonterminalBody));

	if (terminalBody != null)
	    Preconditions.checkArgument(terminalBody != getNonterminal() && terminalBody != nonterminalBody);

	nonterminalBody_ = nonterminalBody;
	terminalBody_ = terminalBody;
    }

    Set<Symbol> getNonterminalSymbols()
    {
	Set<Symbol> nonterminals = super.getNonterminalSymbols();

	if (nonterminalBody_ != null)
	    nonterminals.add(nonterminalBody_);

	return nonterminals;
    }

    Set<Symbol> getTerminalSymbols()
    {
	Set<Symbol> terminals = super.getTerminalSymbols();

	if (terminalBody_ != null)
	    terminals.add(terminalBody_);

	return terminals;
    }
}
